package com.dense.kuiniu.floatcabinet.mvp.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.dense.kuiniu.floatcabinet.R;
import com.dense.kuiniu.floatcabinet.di.component.DaggerQueryComponent;
import com.dense.kuiniu.floatcabinet.di.module.QueryModule;
import com.dense.kuiniu.floatcabinet.mvp.contract.QueryContract;
import com.dense.kuiniu.floatcabinet.mvp.event.BackResultEvent;
import com.dense.kuiniu.floatcabinet.mvp.event.BorrowResultEvent;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.Book;
import com.dense.kuiniu.floatcabinet.mvp.presenter.QueryPresenter;
import com.dense.kuiniu.floatcabinet.mvp.ui.adapter.BookQueryAdapter;
import com.dense.kuiniu.floatcabinet.mvp.ui.view.DialogBorrowResult;
import com.dense.kuiniu.floatcabinet.mvp.ui.view.DialogMessage;
import com.dense.kuiniu.floatcabinet.mvp.ui.view.DialogPutCard;
import com.dense.kuiniu.floatcabinet.mvp.ui.view.SpaceItemDecoration;
import com.dense.kuiniu.floatcabinet.utils.timer.CountDownView;
import com.dense.kuiniu.floatcabinet.utils.timer.CounterController;
import com.jess.arms.di.component.AppComponent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 * @date 2020/10/29
 */
public class QueryActivity extends ImmerseActivity<QueryPresenter> implements QueryContract.View {
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.et_search)
    EditText mEtSearch;
    @BindView(R.id.view_count_down)
    CountDownView mCountView;
    @Inject
    BookQueryAdapter adapter;
    private String mSearchKey;
    private DialogMessage mDialogMessage;
    private DialogBorrowResult dialogBorrowResult;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerQueryComponent.builder()
                .appComponent(appComponent)
                .queryModule(new QueryModule(this))
                .build().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_query;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                mDialogMessage = new DialogMessage(QueryActivity.this);
                dialogBorrowResult = new DialogBorrowResult(QueryActivity.this);
                mCountView.start(new CounterController.OnCompleteListener() {
                    @Override
                    public void onComplete() {
                        finish();
                    }
                });
                initRecyclerView();
                search();
            }
        });

    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(20));
        mRecyclerView.setAdapter(adapter);
        adapter.addChildClickViewIds(R.id.tv_open);
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                Book book = (Book) adapter.getItem(position);
                DialogPutCard dialogPutCard = new DialogPutCard(QueryActivity.this);
                dialogPutCard.show(new DialogPutCard.OnCardReadListener() {
                    @Override
                    public void onComplete(String cardNo) {
                        mEtSearch.setText(mSearchKey);
                        if (mPresenter != null) {
                            mPresenter.authentication(book, cardNo);
                        }
                    }
                });
            }
        });
    }

    @OnClick({R.id.tv_back, R.id.iv_search})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_back:
                finishAfterTransition();
                break;
            case R.id.iv_search:
                search();
                break;
        }
    }

    private void search() {
        if (mPresenter != null) {
            mSearchKey = mEtSearch.getText().toString();
            mPresenter.query(mSearchKey);
        }
    }


    @Override
    public void showData(List<Book> list) {
        if (adapter != null) {
            adapter.setNewInstance(list);
        }
    }

    @Override
    public void showInfo(String msg) {
        mDialogMessage.justInfo(msg).show();
    }


    @Override
    public void showMessage(@NonNull String message) {
        mDialogMessage.justInfoWithConfirm(message)
                .show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 0)
    public void borrowResult(BorrowResultEvent event) {
        if (dialogBorrowResult != null) {
            dialogBorrowResult.setBorrowResult(event.list);
            dialogBorrowResult.show();
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 0)
    public void backResult(BackResultEvent event) {
        if (dialogBorrowResult != null) {
            dialogBorrowResult.setBackResult(event.list);
            dialogBorrowResult.show();
        }
    }


}